package com.gzx.plugin.website.mediator.service.impl;

import com.baomidou.dynamic.datasource.annotation.DS;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.gzx.plugin.website.configs.entity.GzxWebsiteConfig;
import com.gzx.plugin.website.configs.mapper.GzxWebsiteConfigMapper;
import com.gzx.plugin.website.mediator.bo.GzxWebsiteMediatorAddOrEditBo;
import com.gzx.plugin.website.mediator.bo.GzxWebsiteMediatorOutQueryBo;
import com.gzx.plugin.website.mediator.bo.GzxWebsiteMediatorQueryBo;
import com.gzx.plugin.website.mediator.entity.GzxWebsiteMediator;
import com.gzx.plugin.website.mediator.mapper.GzxWebsiteMediatorMapper;
import com.gzx.plugin.website.mediator.service.GzxWebsiteMediatorService;
import com.gzx.plugin.website.mediator.vo.GzxWebsiteMediatorOutVo;
import com.gzx.plugin.website.mediator.vo.GzxWebsiteMediatorVo;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import vip.xiaonuo.common.mvc.service.impl.BaseServiceImpl;

import java.util.List;

/**
 * @author zjt
 * @description
 * @date 2025/4/27 11:00
 */
@Service
@DS("website")
public class GzxWebsiteMediatorServiceImpl extends BaseServiceImpl<GzxWebsiteMediatorAddOrEditBo, GzxWebsiteMediatorVo, GzxWebsiteMediatorQueryBo, GzxWebsiteMediatorMapper, GzxWebsiteMediator> implements GzxWebsiteMediatorService {
    private final GzxWebsiteConfigMapper websiteConfigMapper;

    public GzxWebsiteMediatorServiceImpl(GzxWebsiteMediatorMapper baseMapper, GzxWebsiteConfigMapper websiteConfigMapper) {
        super(baseMapper);
        this.websiteConfigMapper = websiteConfigMapper;
    }

    @Override
    protected LambdaQueryWrapper<GzxWebsiteMediator> buildQueryWrapper(GzxWebsiteMediatorQueryBo queryBo) {
        return Wrappers.<GzxWebsiteMediator>lambdaQuery().orderByDesc(GzxWebsiteMediator::getId);
    }

    /**
     * 官网查询调解员名录
     *
     * @param websiteMediatorOutQueryBo
     * @return
     */
    @Override
    public List<GzxWebsiteMediatorOutVo> pageListOut(GzxWebsiteMediatorOutQueryBo websiteMediatorOutQueryBo) {
        GzxWebsiteConfig websiteConfig = websiteConfigMapper.selectOne(Wrappers.<GzxWebsiteConfig>lambdaQuery()
                .select(GzxWebsiteConfig::getId)
                .eq(GzxWebsiteConfig::getHost, websiteMediatorOutQueryBo.getJkl()));
        if (websiteConfig != null) {
            List<GzxWebsiteMediator> websiteMediatorList = baseMapper.selectList(Wrappers.<GzxWebsiteMediator>lambdaQuery()
                    .eq(GzxWebsiteMediator::getConfigId, websiteConfig.getId())
                    .orderByAsc(GzxWebsiteMediator::getId));
            if (CollectionUtils.isNotEmpty(websiteMediatorList)) {
                return websiteMediatorList.stream()
                        .map(mediator -> {
                            GzxWebsiteMediatorOutVo vo = new GzxWebsiteMediatorOutVo();
                            BeanUtils.copyProperties(mediator, vo);
                            return vo;
                        }).toList();
            }
        }
        return List.of();
    }
}
